import 'package:android_dynamic_icon/android_dynamic_icon.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/components/diolog/dialog.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/http/http_service.dart';
import 'package:pornhub_app/model/announcement/announcement.dart';
import 'package:pornhub_app/model/mine/mine_tabconfigs_model.dart';
import 'package:pornhub_app/routes/routes.dart';
import 'package:pornhub_app/utils/ad_jump.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:tuple/tuple.dart';

import '../../services/user_service.dart';
import 'frontpage/account_credentials_page.dart';
import 'frontpage/change_icon_pop.dart';

class MinePageController extends GetxController {
  final userService = Get.find<UserService>();
  var isFirstShow = false.obs;
  List<Tuple2> hideApps = [];
  List<Tuple2> myFunctions = [];

  ///活动
  Rx<MineTabconfigsModel> activityConfig = MineTabconfigsModel().obs;

  ///促销
  Rx<MineTabconfigsModel> promotionConfig = MineTabconfigsModel().obs;

  //切换图标
  final List<String> iconList = [
    'MainActivity',
    'IconOne',
    'IconTwo',
    'IconThree',
    'IconFour',
    'IconFive',
    'IconSix',
    'IconSeven'
  ];
  String appUrl = "";

  @override
  void onInit() {
    super.onInit();
    initViewDatas();
    getConfig();
    getAppUrl();
    if (GetPlatform.isAndroid) {
      if (!isFirstShow.value &&
          (userService.user.account != null &&
              userService.user.account!.isNotEmpty)) {
        // showAccountCredentialsDialog(Get.context!);
        isFirstShow.value == true;
      }
    }
  }

  void initViewDatas() {
    ///加载隐藏APP
    if (GetPlatform.isAndroid) {
      hideApps
          .add(const Tuple2(AppImagePath.mine_img_change_desktop_one, "计算器"));
      hideApps
          .add(const Tuple2(AppImagePath.mine_img_change_desktop_two, "网易云"));
      hideApps.add(
          const Tuple2(AppImagePath.mine_img_change_desktop_three, "球球英雄"));
      hideApps
          .add(const Tuple2(AppImagePath.mine_img_change_desktop_four, "图片处理"));
      hideApps
          .add(const Tuple2(AppImagePath.mine_img_change_desktop_five, "第五人格"));
    }

    ///加载 我的 相关的功能
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_money, "金币充值"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_publish, "我的发布"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_follow, "我的喜欢"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_message, "我的消息"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_vip, "会员兑换"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_share, "分享推广"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_custom_fans, "官方粉丝群"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_custom, "在线客服"));
    myFunctions.add(const Tuple2(AppImagePath.mine_mine_custom, "视频测试"));
  }

  onClick(String title) {
    switch (title) {
      case '设置':
        // Get.toNamed(Routes.settingpage);
        Get.toNamed(Routes.edituserinfo);
        break;
      case '签到':
        Get.toShare(tabIndex: 1);
        break;
      case '登录':
        Get.toNamed(Routes.loginregist);
        break;
      case '编辑资料':
        Get.toNamed(Routes.edituserinfo);
        break;
      case '官方粉丝群':
        Get.toNamed(Routes.mine_fans_followers, parameters: {'title': "官方粉丝群"});
        break;
      case '关注':
        Get.toNamed(Routes.mine_fans_followers, parameters: {'title': "关注"});
        break;
      case 'VIP':
        Get.toVip(tabInitIndex: 0);
        break;
      case '金币充值':
        Get.toVip(tabInitIndex: 1);
        break;
      case '分享推广':
        Get.toShare();
        break;
      case '我的发布':
        Get.toNamed(Routes.minesrelease);
        break;
      case '我的购买':
        Get.toNamed(Routes.buy);
        break;
      case '我的下载':
        Get.toNamed(Routes.download);
        break;
      case '我的消息':
        Get.toNamed(Routes.minemessage);
        break;
      case '我的喜欢':
        Get.toNamed(Routes.favorite);
        break;
      case '在线客服':
        kOnLineService();
        break;
      case '应用中心':
        if (appUrl.isEmpty) {
          showToast("暂无应用推荐");
        } else {
          jumpExternalAddress(appUrl, null);
        }
        break;
      case '活动':
        Get.toNamed(Routes.groupbuymemberspage);
        break;
      case '兑换会员':
        Get.toNamed(Routes.exchangevippage);
        break;
      case '会员兑换':
        Get.toNamed(Routes.mine_vip_exchange);
        break;
      case '邀请码':
        Get.toNamed(Routes.invitecodepage);
        break;
      case '视频测试':
        Get.toPlayVideo(videoId: 14837);
        break;
      default:
        break;
    }
  }

  showAccountCredentialsDialog(BuildContext context) {
    SmartDialog.show(
      clickMaskDismiss: true,
      builder: (context) {
        return const AccountCredentialsPage();
      },
    );
  }

  Future getConfig() async {
    try {
      final configs = await httpInstance.get(
          url: "sys/mineConfig", complete: MineTabconfigsModel.fromJson);
      if (configs.isEmpty) return;
      for (int i = 0; i < configs.length; i++) {
        if (configs[i].type == 1) {
          activityConfig.value = configs[i];
        } else if (configs[i].type == 2) {
          promotionConfig.value = configs[i];
        }
      }
      update();
    } catch (_) {
      return null;
    }
  }
 
  Future<void> getAppUrl() async {
    final ann = await httpInstance.get<AnnouncementModel>(
        url: 'sys/ann', complete: AnnouncementModel.fromJson);
    if (ann is AnnouncementModel) {
      appUrl = ann.appCenterUrl!;
    }
  }
}
